/* glade-g-script-js-editor.c
 *
 * Copyright (C) 2013 Juan Manuel Mouriz <jmouriz@gmail.com>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <gladeui/glade.h>
#include <gdk/gdkkeysyms.h>
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksource.h>
#include <g-script-js-editor.h>

#include "glade-g-script-js-editor.h"
#include "glade-g-script-js-script.h"

//#include <gladeui/glade-widget-adaptor.h>

static void glade_g_script_js_editor_finalize      (GObject            *object);
static void glade_g_script_js_editor_editable_init (GladeEditableIface *iface);
static void glade_g_script_js_editor_grab_focus    (GtkWidget          *widget);

static GladeEditableIface *parent_editable_iface;

G_DEFINE_TYPE_WITH_CODE (GladeGScriptJsEditor, glade_g_script_js_editor, GTK_TYPE_VBOX,
                         G_IMPLEMENT_INTERFACE (GLADE_TYPE_EDITABLE,
                                                glade_g_script_js_editor_editable_init));

static void
glade_g_script_js_editor_class_init (GladeGScriptJsEditorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->finalize = glade_g_script_js_editor_finalize;
  widget_class->grab_focus = glade_g_script_js_editor_grab_focus;
}

static void
glade_g_script_js_editor_init (GladeGScriptJsEditor *self)
{
}

static void
glade_g_script_js_editor_load (GladeEditable *editable, GladeWidget *widget)
{
  GladeGScriptJsEditor *editor = GLADE_G_SCRIPT_JS_EDITOR (editable);
  GList *properties;

  /* Chain up to default implementation */
  parent_editable_iface->load (editable, widget);

  /* load the embedded editable... */
  if (editor->embed)
  {
    glade_editable_load (GLADE_EDITABLE (editor->embed), widget);
  }

  for (properties = editor->properties; properties; properties = properties->next)
  {
    glade_editor_property_load_by_widget (GLADE_EDITOR_PROPERTY (properties->data), widget);
  }

  if (widget)
  {
  }
}

static void
glade_g_script_js_editor_set_show_name (GladeEditable *editable, gboolean show_name)
{
  GladeGScriptJsEditor *editor = GLADE_G_SCRIPT_JS_EDITOR (editable);

  glade_editable_set_show_name (GLADE_EDITABLE (editor->embed), show_name);
}

static void
glade_g_script_js_editor_editable_init (GladeEditableIface *iface)
{
  parent_editable_iface = g_type_default_interface_peek (GLADE_TYPE_EDITABLE);

  iface->load = glade_g_script_js_editor_load;
  iface->set_show_name = glade_g_script_js_editor_set_show_name;
}

static void
glade_g_script_js_editor_finalize (GObject *object)
{
  GladeGScriptJsEditor *editor = GLADE_G_SCRIPT_JS_EDITOR (object);

  if (editor->properties)
  {
    g_list_free (editor->properties);
  }

  editor->properties = NULL;
  editor->embed = NULL;

  glade_editable_load (GLADE_EDITABLE (object), NULL);

  G_OBJECT_CLASS (glade_g_script_js_editor_parent_class)->finalize (object);
}

static void
glade_g_script_js_editor_grab_focus (GtkWidget *widget)
{
  GladeGScriptJsEditor *editor = GLADE_G_SCRIPT_JS_EDITOR (widget);

  gtk_widget_grab_focus (editor->embed);
}

//-------------------------------//
static void
table_attach (GtkWidget *table, GtkWidget *child, gint position, gint row)
{
  gtk_grid_attach (GTK_GRID (table), child, position, row, 1, 1);

  if (position)
  {
    gtk_widget_set_hexpand (child, TRUE);
  }
}

static void
append_source_editor (GladeGScriptJsEditor *editor, GladeWidgetAdaptor *adaptor)
{
  GladeEditorProperty *eprop, *markup_property;
  GtkWidget *table, *frame, *alignment, *label, *hbox;
  gchar *string;

  /* Title */
  //string = g_strdup_printf ("<b>%s</b>", _("Source Script"));
  string = g_strdup_printf ("<b>%s</b>", "Source Script");
  label = gtk_label_new (string);
  gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
  g_free (string);
  frame = gtk_frame_new (NULL);
  gtk_frame_set_label_widget (GTK_FRAME (frame), label);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
  gtk_box_pack_start (GTK_BOX (editor), frame, FALSE, FALSE, 8);

  alignment = gtk_alignment_new (0.5f, 0.5f, 1.0f, 1.0f);
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 0, 12, 0);
  gtk_container_add (GTK_CONTAINER (frame), alignment);

  table = gtk_grid_new ();
  gtk_orientable_set_orientation (GTK_ORIENTABLE (table), GTK_ORIENTATION_VERTICAL);
  gtk_grid_set_row_spacing (GTK_GRID (table), 4);
  gtk_container_add (GTK_CONTAINER (alignment), table);

  /* Edit the script itself... */
  eprop = glade_widget_adaptor_create_eprop_by_name (adaptor, "script", FALSE, TRUE);
  table_attach (table, glade_editor_property_get_item_label (eprop), 0, 0);
  table_attach (table, GTK_WIDGET (eprop), 1, 0);
  editor->properties = g_list_prepend (editor->properties, eprop);

  /* Edit the source */
  eprop = glade_widget_adaptor_create_eprop_by_name (adaptor, "source", FALSE, TRUE);
  table_attach (table, glade_editor_property_get_item_label (eprop), 0, 1);
  table_attach (table, GTK_WIDGET (eprop), 1, 1);
  editor->properties = g_list_prepend (editor->properties, eprop);

  /* Tie the invisible editor property to the table's life-cycle */
  g_object_set_data_full (G_OBJECT (table), "glade-markup-property-destroy-me",
                          markup_property, (GDestroyNotify) gtk_widget_destroy);
}

GtkWidget *
glade_g_script_js_editor_new (GladeWidgetAdaptor *adaptor, GladeEditable *embed)
{
  GladeGScriptJsEditor *editor;

  g_return_val_if_fail (GLADE_IS_WIDGET_ADAPTOR (adaptor), NULL);
  g_return_val_if_fail (GLADE_IS_EDITABLE (embed), NULL);

  editor = g_object_new (GLADE_TYPE_G_SCRIPT_JS_EDITOR, NULL);
  editor->embed = GTK_WIDGET (embed);

  /* Pack the parent on top... */
  gtk_box_pack_start (GTK_BOX (editor), GTK_WIDGET (embed), FALSE, FALSE, 0);

  append_source_editor (editor, adaptor);

  gtk_widget_show_all (GTK_WIDGET (editor));

  return GTK_WIDGET (editor);
}

GLADE_MAKE_EPROP (GladeEPropScript, glade_eprop_script)

static void
glade_eprop_script_finalize (GObject *object)
{
  /* Chain up */
  GObjectClass *parent_class =
      g_type_class_peek_parent (G_OBJECT_GET_CLASS (object));

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
glade_eprop_script_load (GladeEditorProperty *eprop, GladeProperty *property)
{
  GladeEditorPropertyClass *parent_class = g_type_class_peek_parent (GLADE_EDITOR_PROPERTY_GET_CLASS (eprop));
  GladeEPropScript *script = GLADE_EPROP_SCRIPT (eprop);

  parent_class->load (eprop, property);

  if (script->context)
  {
    glade_name_context_destroy (script->context);
  }

  script->context = NULL;

  if (!property)
  {
    return;
  }

  script->context = glade_name_context_new ();
}

static GtkWidget *
glade_eprop_script_create_input (GladeEditorProperty *eprop)
{
  //GladeEPropScript *script = GLADE_EPROP_SCRIPT (eprop);
  GtkWidget *editor;
  
  editor = g_script_js_editor_new ();

  gtk_widget_show_all (editor);

  return editor;

  /*
  GladeEPropScript *script = GLADE_EPROP_SCRIPT (eprop);
  GtkSourceLanguageManager *manager;
  GtkSourceLanguage *language;
  GtkSourceBuffer *buffer;
  GtkWidget *window;
  GtkWidget *view;

  window = gtk_scrolled_window_new (NULL, NULL);

  gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (window), 150);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (window), GTK_SHADOW_OUT);

  manager = gtk_source_language_manager_get_default ();
  language = gtk_source_language_manager_get_language (manager, "js");
  buffer =  gtk_source_buffer_new_with_language (language);
  script->editor = gtk_source_view_new_with_buffer (buffer);

  gtk_container_add (GTK_CONTAINER (window), script->editor);

  gtk_source_buffer_set_highlight_syntax (buffer, TRUE);
  gtk_source_buffer_set_highlight_matching_brackets (buffer, TRUE);

  gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW (script->editor), TRUE);
  gtk_source_view_set_show_line_marks (GTK_SOURCE_VIEW (script->editor), TRUE);
  gtk_source_view_set_highlight_current_line (GTK_SOURCE_VIEW (script->editor), TRUE);
  gtk_source_view_set_tab_width (GTK_SOURCE_VIEW (script->editor), 8);
  gtk_source_view_set_auto_indent (GTK_SOURCE_VIEW (script->editor), TRUE);
  gtk_source_view_set_insert_spaces_instead_of_tabs (GTK_SOURCE_VIEW (script->editor), TRUE);
  gtk_source_view_set_show_right_margin (GTK_SOURCE_VIEW (script->editor), TRUE);
  gtk_source_view_set_right_margin_position (GTK_SOURCE_VIEW (script->editor), 80);
  gtk_source_view_set_smart_home_end (GTK_SOURCE_VIEW (script->editor), TRUE);

  gtk_widget_show_all (window);

  return window;
  */
}
